package io.mya.modules.sys.service;

import io.mya.modules.sys.dao.UserMapper;
import io.mya.modules.sys.vo.CandyUser;
import io.mya.modules.sys.vo.PrometorUserStatistics;
import io.mya.modules.sys.vo.PrometorUserZTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
public class UserGeneralizeService {
    @Autowired
    UserMapper userMapper;
    final String prefix = "/generalize";

    public String getUserLink(CandyUser user) {
        return prefix + "/" + user.getId();
    }

    public void calcBonus(Long currUser) throws Exception {
        CandyUser user = userMapper.getUser(currUser);
        if (Objects.isNull(user))
            throw new Exception("用户ID " + currUser + "不存在");
        List<Long> ids = new ArrayList<>(6);
        //one
        Long p1Prometor = userMapper.getPrevPrometor(currUser);
        if (Objects.nonNull(p1Prometor) && !p1Prometor.equals(0)) {
            ids.add(p1Prometor);
            //two
            Long p2Prometor = userMapper.getPrevPrometor(p1Prometor);
            if (Objects.nonNull(p2Prometor) && !p2Prometor.equals(0)) {
                ids.add(p2Prometor);
                //three
                Long p3Prometor = userMapper.getPrevPrometor(p2Prometor);
                if (Objects.nonNull(p3Prometor) && !p3Prometor.equals(0)) {
                    ids.add(p3Prometor);
                    //four
                    Long p4Prometor = userMapper.getPrevPrometor(p3Prometor);
                    if (Objects.nonNull(p4Prometor) && !p4Prometor.equals(0)) {
                        ids.add(p4Prometor);
                        //five
                        Long p5Prometor = userMapper.getPrevPrometor(p4Prometor);
                        if (Objects.nonNull(p5Prometor) && !p5Prometor.equals(0)) {
                            ids.add(p5Prometor);
                            //six
                            Long p6Prometor = userMapper.getPrevPrometor(p5Prometor);
                            if (Objects.nonNull(p6Prometor) && !p6Prometor.equals(0))
                                ids.add(p6Prometor);
                        }
                    }
                }
            }
        }
        if (!ids.isEmpty()) {
            IntStream.range(0,ids.size()).forEach(e->{
                int bonusCount = userMapper.getBonusCount(e + 1);
                userMapper.addBonus(ids.get(e),bonusCount);
            });
        }
    }

    /**
     * 获取用户的推荐统计
     *
     * @param id
     * @return
     */
    public PrometorUserStatistics getSubUserStatistics(long id) {
        PrometorUserStatistics prometorUserStatistics = new PrometorUserStatistics();
        CandyUser user = userMapper.getUser(id);
        List<CandyUser> subUser = userMapper.getSubUser(user.getId());
        prometorUserStatistics.setLv1(subUser.size());
        //two
        int lv2 = 0;
        int lv3 = 0;
        int lv4 = 0;
        int lv5 = 0;
        int lv6 = 0;
        for (CandyUser user1 : subUser) {
            List<CandyUser> subUser1 = userMapper.getSubUser(user1.getId());
            if (subUser1 == null || subUser1.isEmpty()) continue;
            //three
            for (CandyUser user2 : subUser1) {
                List<CandyUser> subUser2 = userMapper.getSubUser(user2.getId());
                if (subUser2 == null || subUser2.isEmpty()) continue;
                //four
                for (CandyUser user3 : subUser2) {
                    List<CandyUser> subUser3 = userMapper.getSubUser(user3.getId());
                    if (subUser3 == null || subUser3.isEmpty()) continue;
                    //five
                    for (CandyUser user4 : subUser3) {
                        List<CandyUser> subUser4 = userMapper.getSubUser(user4.getId());
                        if (subUser4 == null || subUser4.isEmpty()) continue;
                        for (CandyUser user5 : subUser4) {
                            List<CandyUser> subUser5 = userMapper.getSubUser(user5.getId());
                            if (subUser5 == null || subUser5.isEmpty()) continue;
                            lv6+=subUser5.size();
//                            prometorUserStatistics.setLv5(subUser5.size());
                        }
                        lv5+=subUser4.size();
//                        prometorUserStatistics.setLv5(subUser4.size());
                    }
                    lv4+=subUser3.size();
//                    prometorUserStatistics.setLv4(subUser3.size());
                }
                lv3+=subUser2.size();
//                prometorUserStatistics.setLv3(subUser2.size());
            }
            lv2+=subUser1.size();
//            prometorUserStatistics.setLv2(subUser1.size());
        }

        prometorUserStatistics.setLv2(lv2);
        prometorUserStatistics.setLv3(lv3);
        prometorUserStatistics.setLv4(lv4);
        prometorUserStatistics.setLv5(lv5);
        prometorUserStatistics.setLv6(lv6);



        return prometorUserStatistics;
    }

    public PrometorUserZTree getUser(long id) {
        PrometorUserZTree prometorUserZTree = new PrometorUserZTree();
        CandyUser user = userMapper.getUser(id);
        prometorUserZTree.setId(user.getId());
        prometorUserZTree.setName(user.getUsername());
        return prometorUserZTree;
    }

    public List<PrometorUserZTree> getSubUser(long id) {
        List<PrometorUserZTree> prometorUserList = new ArrayList<>();
        List<CandyUser> user = userMapper.getSubUser(id);
        return user.stream().map(e -> {
            PrometorUserZTree prometorUser = new PrometorUserZTree();
            prometorUser.setName(e.getUsername());
            prometorUser.setId(e.getId());
            prometorUserList.add(prometorUser);
            return prometorUser;
        }).collect(Collectors.toList());
    }

    public int getUserCandyCount() {
        return userMapper.getUserCandyCount();
    }
}